Skip to content

Using Objects

Objects: Instances of Classes

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Explain the relationship between a class and an object.

ESSENTIAL KNOWLEDGE

  • An object is a specific instance of a class with defined attributes.

  • A class is the formal implementation, or blueprint, of the attributes and behaviors of an object.

Creating and Storing Objects (Instantiation)

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Identify, using its signature, the correct constructor being called.

ESSENTIAL KNOWLEDGE

  • A signature consists of the constructor name and the parameter list.

  • The parameter list, in the header of a constructor, lists the types of the values that are passed and their variable names. These are often referred to as formal parameters.

  • A parameter is a value that is passed into a constructor. These are often referred to as actual parameters.

  • Constructors are said to be overloaded when there are multiple constructors with the same name but a different signature.

  • The actual parameters passed to a constructor must be compatible with the types identified in the formal parameter list.

  • Parameters are passed using call by value. Call by value initializes the formal parameters with copies of the actual parameters.

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

For creating objects:

  • Create objects by calling constructors without parameters.
  • Create objects by calling constructors with parameters.

ESSENTIAL KNOWLEDGE

  • Every object is created using the keyword new followed by a call to one of the class’s constructors.

  • A class contains constructors that are invoked to create objects. They have the same name as the class.

  • Existing classes and class libraries can be utilized as appropriate to create objects.

  • Parameters allow values to be passed to the constructor to establish the initial state of the object.

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Define variables of the correct types to represent reference data.

ESSENTIAL KNOWLEDGE

  • The keyword null is a special value used to indicate that a reference is not associated with any object.

  • The memory associated with a variable of a reference type holds an object reference value or, if there is no object, null. This value is the memory address of the referenced object.

Calling a Void Method

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Call non-static void methods without parameters.

ESSENTIAL KNOWLEDGE

  • An object’s behavior refers to what the object can do (or what can be done to it) and is defined by methods.

  • Procedural abstraction allows a programmer to use a method by knowing what the method does even if they do not know how the method was written.

  • A method signature for a method without parameters consists of the method name and an empty parameter list.

  • A method or constructor call interrupts the sequential execution of statements, causing the program to first execute the statements in the method or constructor before continuing. Once the last statement in the method or constructor has executed or a return statement is executed, flow of control is returned to the point immediately following where the method or constructor was called.

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Call non-static void methods without parameters.

ESSENTIAL KNOWLEDGE

  • Non-static methods are called through objects of the class.

  • The dot operator is used along with the object name to call non-static methods.

  • Void methods do not have return values and are therefore not called as part of an expression.

  • Using a null reference to call a method or access an instance variable causes a NullPointerException to be thrown.

Calling a Void Method with Parameters

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Call non-static void methods with parameters.

ESSENTIAL KNOWLEDGE

  • A method signature for a method with parameters consists of the method name and the ordered list of parameter types.

  • Values provided in the parameter list need to correspond to the order and type in the method signature.

  • Methods are said to be overloaded when there are multiple methods with the same name but a different signature.

Calling a Non-void Method

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Call non-static non-void methods with or without parameters.

ESSENTIAL KNOWLEDGE

  • Non-void methods return a value that is the same type as the return type in the signature. To use the return value when calling a non-void method, it must be stored in a variable or used as part of an expression.

String Objects: Concatenation, Literals, and More

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

For String class:

  • Create String objects.
  • Call String methods.

ESSENTIAL KNOWLEDGE

  • String objects can be created by using string literals or by calling the String class constructor.

  • String objects are immutable, meaning that String methods do not change the String object.

  • String objects can be concatenated using the + or += operator, resulting in a new String object.

  • Primitive values can be concatenated with a String object. This causes implicit conversion of the values to String objects.

  • Escape sequences start with a \ and have a special meaning in Java. Escape sequences used in this course include \”, \, and \n.

String Methods

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

For String class:

  • Create String objects.
  • Call String methods.

ESSENTIAL KNOWLEDGE

  • Application program interfaces (APIs) and libraries simplify complex programming tasks.

  • Documentation for APIs and libraries are essential to understanding the attributes and behaviors of an object of a class.

  • Classes in the APIs and libraries are grouped into packages.

  • The String class is part of the java.lang package. Classes in the java.lang package are available by default.

  • A String object has index values from 0 to length – 1. Attempting to access indices outside this range will result in an StringIndexOutOfBoundsException.

  • A String object can be concatenated with an object reference, which implicitly calls the referenced object’s toString method.

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

For String class:

  • Create String objects.
  • Call String methods.

ESSENTIAL KNOWLEDGE

  • The following String methods and constructors—including what they do and when they are used—are part of the Java Quick Reference:

    • String(String str) — Constructs a new String object that represents the same sequence of characters as str

    • int length()— Returns the number of characters in a String object

    • String substring(int from, int to)— Returns the substring beginning at index from and ending at indexto − 1

    • String substring(int from) — Returns substring(from, length())

    • int indexOf(String str) — Returns the index of the first occurrence of str; returns -1 if not found

    • boolean equals(String other) — Returns true if this is equal to other; returns false otherwise

    • int compareTo(String other) — Returns a value < 0 if this is less than other; returns zero if this is equal to other; returns a value > 0 if this is greater than other

  • A string identical to the single element substring at position index can be created by calling substring(index, index + 1).

Wrapper Classes: Integer and Double

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

For wrapper classes:

  • Create Integer objects.
  • Call Integer methods.
  • Create Double objects.
  • Call Double methods.

ESSENTIAL KNOWLEDGE

  • The Integer class and Double class are part of the java.lang package.

The following Integer methods and constructors—including what they do and when they are used—are part of the Java Quick Reference:

  • Integer(int value) — Constructs a new Integer object that represents the specified int value

  • Integer.MIN_VALUE— The minimum value represented by an int or Integer

  • Integer.MAX_VALUE— The maximum value represented by an int or Integer

  • int intValue()— Returns the value of this Integer as an int

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

For wrapper classes:

  • Create Integer objects.
  • Call Integer methods.
  • Create Double objects.
  • Call Double methods.

ESSENTIAL KNOWLEDGE

  • The following Double methods and constructors—including what they do and when they are used—are part of the Java Quick Reference:

    • Double(double value) — Constructs a new Double object that represents the specified double value
    • double doubleValue()— Returns the value of this Double as a double
  • Autoboxing is the automatic conversion that the Java compiler makes between primitive types and their corresponding object wrapper classes. This includes converting an int to an Integer and a double to a Double.

  • The Java compiler applies autoboxing when a primitive value is:

    • Passed as a parameter to a method that expects an object of the corresponding wrapper class.
    • Assigned to a variable of the corresponding wrapper class.
  • Unboxing is the automatic conversion that the Java compiler makes from the wrapper class to the primitive type. This includes converting an Integer to an int and a Double to a double.

  • The Java compiler applies unboxing when a wrapper class object is:

    • Passed as a parameter to a method that expects a value of the corresponding primitive type.
    • Assigned to a variable of the corresponding primitive type.

Using the Math Class

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Call static methods.

ESSENTIAL KNOWLEDGE

Static methods are called using the dot operator along with the class name unless they are defined in the enclosing class.

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Evaluate expressions that use the Math class methods.

ESSENTIAL KNOWLEDGE

  • The Math class is part of the java.lang package.

  • The Math class contains only static methods.